<!-- home -->
<template>
  <div class="index-container clearfix">
    <div class="header"></div>
    <div class="main">
      <div class="title">{{ parseCut(cutValue).title }}</div>

      <van-form @submit="onSubmit">
        <van-field
          v-model="state.account"
          name="手机号"
          :left-icon="require('@/assets/images/log-user.png')"
          clearable
          placeholder="输入你的手机号"
          :rules="[
            { required: true, message: '请填写手机号' },
            { pattern: /^1\d{10}$/, message: '手机号码格式错误！' }
          ]"
        />
        <van-field
          v-if="cutValue == 1"
          v-model="state.password"
          :left-icon="require('@/assets/images/log-pas.png')"
          type="password"
          clearable
          name="密码"
          placeholder="输入你的密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <van-field
          v-else
          v-model="state.smscode"
          center
          maxlength="6"
          :left-icon="require('@/assets/images/log-pas.png')"
          clearable
          type="number"
          name="短信验证码"
          placeholder="请输入短信验证码"
          :rules="[
            { required: true, message: '请填写短信验证码' },
            { pattern: /^[0-9]{6}$/, message: '验证码必须为6位数字' }
          ]"
        >
          <template #button>
            <van-button
              style="border: 0; color: #009697"
              size="small"
              :loading="loadingSms"
              :disabled="Two"
              native-type="button"
              @click="duanxin"
            >
              | 获取验证码
              <span v-show="Two">{{ nuLL }}</span>
            </van-button>
          </template>
        </van-field>
        <div class="cut" @click="hadleCut">{{ parseCut(cutValue).lable }}</div>
        <div class="queren">
          <van-button type="primary" :loading="loading" native-type="submit">登录</van-button>
          <div
            v-if="cutValue != 3"
            class="van-button mt15"
            style="border: 1px solid #cccccc; background: #ffffff; color: #666666"
            @click="cutValue = 3"
          >没有账号立即注册</div>
        </div>
        <div v-if="cutValue == 3" class="div_tips">
          注册即代表同意王派医学协议
          <a href="http://h5.wangpaiyixue.cn/user-agreement">《注册协议》</a>
        </div>
      </van-form>
    </div>
    <!-- <van-checkbox v-model="checked" label-disabled icon-size="14px" class="div_tips">
      登录注册即表示同意和
      <a href="http://h5.wangpaiyixue.cn/user-agreement">《用户注册协议》</a> 和
      <a href="http://h5.wangpaiyixue.cn/privacy-policy">《隐私策略》</a>
    </van-checkbox>-->
  </div>
</template>

<script>
// import { getCode } from '@/utils'
import { Toast } from 'vant'
import { getSms } from '@/api/user'
export default {
  data() {
    return {
      list: {},
      loading: false,
      loadingSms: false,
      cutValue: 1,
      state: {
        type: 'phonepwd',
        account: '',
        password: '',
        smscode: '',
        share_token: '',
        code: ''
      },
      nuLL: 60,
      Two: false,
      checked: true,
      code: ''
    }
  },

  computed: {},

  mounted() {
    // this.state.code = getCode()
  },
  created() { },

  methods: {
    hadleCut() {
      this.cutValue = this.parseCut(this.cutValue).value
    },
    onSubmit() {
      this.$store
        .dispatch('user/login', this.state)
        .then(res => {
          console.log(res)
          this.$router.push('/mine')
          // if (res.member_type == 'member' && !res.profile.city) {
          //   this.$router.push('/preparer')
          // } else {
          //   if (sessionStorage.getItem('formHref')) {
          //     window.location.href = sessionStorage.getItem('formHref')
          //   } else {
          //     this.$router.push('/mine')
          //     this.$store.dispatch('defaultActive', 2)
          //   }
          // }
          // localStorage.setItem('member_type', res.member_type)
          this.loading = false
        })
        .catch(_ => {
          this.loading = false
        })
    },
    // 倒计时
    djshi() {
      var num = setInterval(() => {
        this.Two = true
        this.nuLL -= 1
        if (this.nuLL < 1) {
          clearInterval(num)
          if (this.nuLL < 1) {
            this.Two = false
            this.nuLL = 60
          }
        }
      }, 1000)
    },
    duanxin() {
      if (!this.state.account) {
        return false
      }
      this.loadingSms = true
      const data = {
        phone: this.state.account,
        behavior: 'login'
      }
      getSms(data)
        .then(res => {
          Toast({
            message: '短信发送成功，请注意查收',
            position: 'top'
          })
          this.djshi()
        })
        .catch(res => {
          Toast({
            message: res.msg,
            position: 'top'
          })
        })
        .finally(() => {
          this.loadingSms = false
        })
    },
    parseCut(cut) {
      switch (cut) {
        case 1:
          return { lable: '点击验证码登录', title: '密码登录', value: 2 }
        case 2:
          return { lable: '点击密码登录', title: '验证码登录', value: 1 }
        case 3:
          return { lable: '已有账号立即登录', title: '注册账号', value: 1 }
        default:
          ''
      }
    }
  }
}
</script>
<style lang="scss" scoped>
h2 {
  margin-top: 10px;
  font-size: 15px;
  font-weight: bold;
  color: #333333;
}
.index-container {
  width: 100%;
  height: 100%;
  background: #ffffff;
  .header {
    width: 158px;
    height: 50px;
    margin: 64px auto 0;
    background: url('@/assets/images/logo.png');
    background-size: 100% 100%;
  }
  .main {
    box-sizing: border-box;
    width: 100%;
    margin-top: 50px;
    padding: 0 34px;
    background: #ffffff;
    border-radius: 20px;
    .title {
      font-size: 15px;
      font-weight: 400;
      color: #009697;
      line-height: 20px;
    }
    .cut {
      margin-top: 15px;
      text-align: right;
      font-size: 15px;
      font-weight: 400;
      color: #333333;
      line-height: 20px;
    }
    ::v-deep .van-form {
      margin-top: 25px;
      .van-cell {
        margin-top: 15px;
        padding: 15px 0;
      }
      .van-cell__value {
        font-size: 17px;
        font-weight: 400;
        color: #999999;
        line-height: 20px;
      }
      .van-icon {
        font-size: 18px;
      }
      .van-field__left-icon {
        margin-right: 27px;
      }
    }
  }
  .div_tips {
    margin-top: 15px;
    font-size: 13px;
    font-weight: 400;
    color: #999999;
    line-height: 39px;
    text-align: center;
    // display: flex;
    a {
      color: #ff3232;
    }
  }

  .queren {
    margin: 45px auto 0;
    .van-button {
      display: block;
      width: 100%;
      height: 45px;
      line-height: 45px;
      background: #009697;
      border-radius: 10px;
      font-size: 17px;
      font-weight: 400;
      color: #ffffff;
      border: none;
    }
  }
}
</style>
